package com.hskn.hss.module.costdict.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.casesetlevel.entity.CasesetLevel;
import com.hskn.hss.module.clock.mapper.ClockMapper;
import com.hskn.hss.module.costdict.entity.CostDict;
import com.hskn.hss.module.costdict.mapper.CostDictMapper;
import com.hskn.hss.module.costdict.service.ICostDictService;
import com.hskn.hss.module.costdictitem.entity.CostDictItem;
import com.hskn.hss.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-05
 */
@Slf4j
@RestController
@RequestMapping("/hss/costdict")
public class CostDictController {

    @Autowired
    private ICostDictService costDictService;

    /***
     * 新增信息
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.INSERT, tableName = "cost_dict", bean = CostDict.class, beanMapper = CostDictMapper.class, names = "costType")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public AjaxResult<Object> insert(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        return costDictService.insert(params);
    }

    /**
     * 修改信息
     *
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.UPDATE, tableName = "cost_dict", idKey = "id", bean = CostDict.class, beanMapper = CostDictMapper.class, names = "costtype")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        return costDictService.update(params);
    }

    /***
     * 删除角色信息
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.DELETE, tableName = "cost_dict", idKey = "id", bean = CostDict.class, beanMapper = CostDictMapper.class, names = "costtype")
    @RequestMapping(value = "deleteByid", method = RequestMethod.POST)
    public AjaxResult<Object> deleteByid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        return costDictService.deleteByid(params.getString("id"));
    }

    /**
     * 根据Id获取信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getitemByid", method = RequestMethod.POST)
    public AjaxResult<Object> getitemByid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        CostDict CostDict = null;
        try {
            CostDict = costDictService.getitemByid(params.getString("id"));
            return AjaxResult.success(CostDict);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }

    /**
     * 根据menuid获取列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getitemListByParentId", method = RequestMethod.POST)
    public AjaxResult<Object> getitemListByParentId(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            List<CostDict> costDictList = costDictService.getitemListByParentId(params.getString("menuid"));
            return AjaxResult.success(costDictList);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }

    /**
     * 根据menuid获取列表
     *
     * @param costDict
     * @return
     */
    @RequestMapping(value = "getitemList", method = RequestMethod.POST)
    public AjaxResult<Object> getitemList(@RequestBody CostDict costDict) {
        try {
            List<CostDictItem> costDictList = costDictService.getitemList(costDict);
            return AjaxResult.success(costDictList);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
    }
}

